﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    /// <summary>
    /// 全局实体创建工厂，用于创建相关实体[该工厂同时会负责将对应实体注册到根实体列表中]
    /// </summary>
    public static class EntityFactory
    {

        /// <summary>
        /// 组装指定实体
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <returns>组装完成的组件</returns>
        public static T Create<T>() 
            where T : Entity
        {
            T component = GameRoot.ObjectPool.Fetch<T>();
            Entity entity = component as Entity;
            if (entity != null)
            {
                entity.InstID = component.InstId;
            }
            GameRoot.DispatcherSystem.AddEntity(entity);
            return component;
        }
        /// <summary>
        /// 组装指定实体并使用指定的GID
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="id">指定的GID</param>
        /// <returns>组装完成的实体</returns>
        public static T CreateWithId<T>(long id) 
            where T : ComponentSuperior
        {
            T component = GameRoot.ObjectPool.Fetch<T>();
            Entity entity = component as Entity;
            if (entity != null)
            {
                entity.InstID = id;
            }
            GameRoot.DispatcherSystem.AddEntity(entity);
            return component;
        }
    }
}
